<?php

/**
 * This is the model class for table "{{post}}".
 *
 * The followings are the available columns in table '{{post}}':
 * @property integer $id
 * @property integer $topic_id
 * @property string $message
 * @property integer $total_post
 * @property integer $total_view
 * @property integer $status_code
 * @property string $create_timezone
 * @property string $create_timestamp
 * @property string $create_datetime
 * @property string $create_date
 * @property string $create_time
 * @property integer $create_by
 * @property string $update_timezone
 * @property string $update_timestamp
 * @property string $update_datetime
 * @property string $update_date
 * @property string $update_time
 * @property integer $update_by
 */
class Post extends ActiveRecord
{
	public $topic_;
	public $message_;
	public $status_;
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{post}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('id_, message_', 'filter', 'filter'=>'trim'),
			array('image_', 'file', 'allowEmpty'=>true, 'types'=>'gif,jpg,png', 'maxSize'=>Yii::app()->params['maxImageSize']),
			
			array('message_', 'required'),
			array('message_', 'length', 'max'=>500),
			array('id_, topic_, message_, status_', 'safe', 'on' => 'userSearch, userCreate, userUpdate, adminSearch, adminCreate, adminUpdate'),
			
			// compulsory
			array('var_', 'ext.validators.BotDetectionValidator'),
			array('var_', 'safe'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'creator' => array(self::BELONGS_TO, 'User', 'create_by'),
			'editor' => array(self::BELONGS_TO, 'User', 'update_by'),
			'photo' => array(self::HAS_ONE, 'Gallery', 'module_id', 'scopes' => array('module' => __CLASS__, 'primary', 'accessible')),
			'gallery' => array(self::HAS_MANY, 'Gallery', 'module_id', 'scopes' => array('module' => __CLASS__, 'accessible')),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'content' => 'Content',
			'create_timezone' => 'Create Time Zone',
			'create_timestamp' => 'Create Timestamp',
			'create_datetime' => 'Create Datetime',
			'create_date' => 'Create Date',
			'create_time' => 'Create Time',
			'create_by' => 'Create By',
			'update_timezone' => 'Update Time Zone',
			'update_timestamp' => 'Update Timestamp',
			'update_datetime' => 'Update Datetime',
			'update_date' => 'Update Date',
			'update_time' => 'Update Time',
			'update_by' => 'Update By',
		);
	}
	
	public function scopes() {
		$t=$this->getTableAlias(false);
		return array(
			'published' => array(
					'condition' => $t.'.status_code = :activeStatus'.$t,
					'params' => array(
							':activeStatus'.$t => Option::STATUS_ACTIVE,
					),
			),
			'accessible'=>array(
					'condition' => $t.'.status_code <> :deletedStatus'.$t,
					'params' => array(
							':deletedStatus'.$t=>Option::STATUS_DELETED,
					),
			),
		);
	}
	
	public function autoFill() {
		switch($this->scenario){
			case 'insert':
			case 'adminCreate':
			case 'userCreate':
				$this->status_ = Option::STATUS_ACTIVE;
				break;
			case 'update':
			case 'adminUpdate':
			case 'userUpdate':
				$this->status_ = $this->status_code;
				$this->topic_ = $this->topic_id;
				$this->message_ = $this->message;
				break;
		}
	}

	protected function beforeSave() {
		if(parent::beforeSave()){
			
            $this->status_code=$this->status_;
            $this->topic_id = $this->topic_;
			$this->message = $this->message_;
            
            if ($this->isNewRecord) {
            	//$this->total_view = 0;
            	$this->setCreateDateTime();
            } else {
            	$this->setUpdateDateTime();
            }
            return true;
        }else
            return false;
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function userSearch()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$t = $this->getTableAlias(false);
		$criteria = new CDbCriteria;
		$criteria->with=array('creator');

		$criteria->compare($t.'.topic_id', $this->topic_);
		
		return new ActiveDataProvider($this->published(), array(
			'criteria'=>$criteria,
			'pagination' => array(
				'pageSize' => Yii::app()->request->getParam('pageSize_', $this->pageSize_),
				'pageVar' => 'page_',
			),
		));
	}
	
	public function adminSearch(){
		$t = $this->getTableAlias(false);
		$criteria = new CDbCriteria;
		$criteria->with=array('creator');
		
		$criteria->compare($t.'.topic_id', $this->topic_);
		
		return new ActiveDataProvider($this->accessible(), array(
				'criteria' => $criteria,
				'pagination' => array(
					'pageSize' => Yii::app()->request->getParam('pageSize_', $this->pageSize_),
					'pageVar' => 'page_',
				),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Post the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
